let sd = require('silly-datetime');

module.exports = {
    
    //格式化时间
    formatTime(str, time) {
        var d = new Date();
        if(typeof(time) == 'number'){
            d = new Date(time * 1000);
        }

        if(typeof(time) == 'string'){
            d = new Date(time);
        }

        if(typeof(time) == 'object'){
            d = time;
        }

        if(!str){
            str = "YYYY-MM-DD HH:mm:ss";
        }
        return sd.format(d, str);
    },

    msFormatTime(str, time){
        time = time / 1000;
        return this.formatTime(str, time);
    },

    //获取当前时间轴
    getTime(flag, time){
        var now = new Date();
        if(time){
            now = new Date(time);
        }
        if(flag){
            //flag 为真，输出毫秒时间
            return now.getTime();
        } else {
            return Math.floor(now.getTime()/1000);
        }
    },


    /**
     * 获取page信息
     * @returns 
     */
    getPageInfo(){
        let { ctx } = this;

        let params = {
            ...ctx.query,
            ...ctx.params,
            ...ctx.request.body
        }

        let page = parseInt(params.page) || 1;
        let pageSize = parseInt(params.page_size) || 10;
        let offset = (page - 1) * pageSize;

        return {
            page: page,
            pageSize: pageSize,
            offset: offset
        };
    },


    //下划线对象转驼峰格式
    toCamel(data, flag){
        let beforeData = data;
        if(data.dataValues && !flag){
            beforeData = data.dataValues;
        }

        let rsData = {};
        for( let key in beforeData){
            let value = beforeData[key];

            if(key.slice(0,1) === '_'){ 
                key = key.slice(1);
            }
            let newKey = key.replace(/([^_])(?:_+([^_]))/g, function ($0, $1, $2) {
                return $1 + $2.toUpperCase();
            });

            rsData[newKey] = value;
        }

        return rsData
    },

    //驼峰对象转下划线
    toUnderScoreCase(data){

    },

    getProjectStatusLabel(status){
        let label="";

        switch(status){
            case 0:
                label= "筹备中";
                break;
            case 1:
                label= "开发中";
                break;
            case 2:
                label= "暂停";
                break;
            case 10:
                label= "结束";
                break;
            case 11:
                label= "放弃";
                break
        }
        
        return label;
    }
};